import { http } from '@/utils/http'
import {ICaptcha, IUserLogin, LoginBaseData, LoginFormData} from "@/types/login";

export function useLoginApi() {
  return {
    /**
     * 用户登录
     * @param loginForm 登录表单
     */
    login(loginForm: LoginFormData) {
      return http.post<IUserLogin>(
        '/passkey-api/sign/login/signin?_allow_anonymous=true',
        loginForm
      )
    },

    /**
     * 退出登录
     */
    logout() {
      return http.get<void>('/passkey-api/logout')
    },

    /**
     * 获取微信登录凭证
     * @returns Promise 包含微信登录凭证(code)
     */
    getWxCode() {
      return new Promise<UniApp.LoginRes>((resolve, reject) => {
        uni.login({
          provider: 'weixin',
          success: (res) => resolve(res),
          fail: (err) => reject(new Error(err)),
        })
      })
    },

    /**
     * 微信登录
     * @returns Promise 包含登录结果
     * @param data
     */
    wxLogin(data: { code: string }) {
      return http.post<IUserLogin>('/user/wxLogin', data)
    },

    /**
     * 获取基础认证信息
     * @returns LoginBaseData 登录基础信息
     */
    signLoginGet() {
      return http.get<LoginBaseData>('/passkey-api/sign/login/get?_allow_anonymous=true&remember_me=true')
    },

    /**
     * 获取验证码
     * @returns ICaptcha 验证码
     */
    getCaptcha(query: { state: string, captcha: string }) {
      return http.get<ICaptcha>('/passkey-api/sign/captcha', query)
    },
    test() {
      return http.get('/passkey-api/sign/authz/oauth/v20/authorize/approval?user_oauth_approval=true&client_id=1117901809654956032')
    }
  }
}
